package com.phx.serialjni;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.utils.PakageMod;

import java.util.ArrayList;
import java.util.List;

public class gActivity extends Activity {

    private GridView gridview;
    private PackageManager pManager;
    private List<PakageMod> datas;
    private List<PakageMod> imgs;
    private String tag = "gActivity";
    private String[] datax;
    private int[] imgids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_maing);
        // 取得gridview
        gridview = (GridView) findViewById(R.id.gridview1);
        // 获取图片、应用名、包名
        pManager = gActivity.this.getPackageManager();
        List<PackageInfo> appList = getAllApps(gActivity.this);
        datas = new ArrayList<PakageMod>();
       int sizex = appList.size();
        datax = new String[sizex];
        imgids = new int[sizex];
        for (int i = 0; i < appList.size(); i++) {
            PackageInfo pinfo = appList.get(i);
            PakageMod shareItem = new PakageMod();
            // 设置图片
            shareItem.icon = pManager.getApplicationIcon(pinfo.applicationInfo);
           // Resources resources=pm.getResourcesForApplication(PackageName);
            // 设置应用程序名字
            shareItem.appName = pManager.getApplicationLabel(
                    pinfo.applicationInfo).toString();
            // 设置应用程序的包名
            shareItem.pakageName = pinfo.applicationInfo.packageName;

           // Resources resources=pinfo..getResourcesForApplication(shareItem.pakageName);
                    //(shareItem.pakageName);
          //  Resources resources = pinfo.applicationInfo.get
         // int indentify = resources.getIdentifier(pinfo.applicationInfo.icon,null,null);


            datas.add(shareItem);
            datax[i]= shareItem.appName;
            imgids[i] = pinfo.applicationInfo.icon;

        }

        gridview.setAdapter(new DemoGridAdapter(this,datas));

        // 点击应用图标时，做出响应
        gridview.setOnItemClickListener(new ClickListener());
    }

    public static List<PackageInfo> getAllApps(Context context) {

        List<PackageInfo> apps = new ArrayList<PackageInfo>();
        PackageManager pManager = context.getPackageManager();
        // 获取手机内所有应用
        List<PackageInfo> packlist = pManager.getInstalledPackages(0);
        for (int i = 0; i < packlist.size(); i++) {
            PackageInfo pak = (PackageInfo) packlist.get(i);
            // if()里的值如果<=0则为自己装的程序，否则为系统工程自带
            if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
                // 添加自己已经安装的应用程序
                 apps.add(pak);
            }
           // apps.add(pak);
        }
        return apps;
    }

    private class ClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            Intent intent = new Intent();
            intent = gActivity.this.getPackageManager()
                    .getLaunchIntentForPackage(datas.get(position).pakageName);
            startActivity(intent);
        }
    }
}